/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package bhoost.shell;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.CameraDevice;
import android.os.Bundle;
import android.widget.Toast;


public class CameraTest extends Activity {

	private int width = 1280;
	private int height = 960;

	private CameraDevice device = null;
	String filePath = "/tmp/camerashot1.jpeg";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        if (open()) {
        	takePicture();
        	close();
        	Toast.makeText(this, "picture taken: " + filePath, Toast.LENGTH_LONG).show();
        	finish();
        }
    }

    public void takePicture() {
    	Canvas c = new Canvas();
    	Bitmap bmp = Bitmap.createBitmap(width, height, false);
    	c.setDevice(bmp);
    	
    	if (!capture(c))
    		return;
    	
    	// draw timestamp on image
    	Paint paintText = new Paint();
    	paintText.setTextSize(50);
    	paintText.setColor(Color.RED);
        paintText.setAntiAlias(true); 
        c.drawText("TIMESTAMP: " + new Date(System.currentTimeMillis()).toString(), 50, 50, paintText);

    	try {
    		File image = new File(filePath);
    		if (image.exists())
    			image.delete();
    		
	    	FileOutputStream fs = new FileOutputStream(image);
	    	bmp.compress(Bitmap.CompressFormat.JPEG, 50, fs);
	    	fs.close();
    	} catch (FileNotFoundException f) {
    		f.printStackTrace();
    	} catch (IOException i) {
    		i.printStackTrace();
    	}
    }

	public boolean open() {
		if (device != null) return true;
		device = CameraDevice.open();
		if (device == null) return false;
		
		//parameters for the device mostly as specified in sample app
		CameraDevice.CaptureParams param = new CameraDevice.CaptureParams();
		param.type = 1; // preview
		param.srcWidth = width;
		param.srcHeight = height;
		param.leftPixel = 0;
		param.topPixel = 0;
		param.outputWidth = width;
		param.outputHeight = height;
		param.dataFormat = 2; // RGB_565
	
		//attempt to configure the device here
		if (!device.setCaptureParams(param)) {
			device.close();
			device = null;
			return false;
		}
		
		return true;
	}
	
	public void close() {
		if (device == null) return;
		device.close();
		device = null;
	}
	
	public boolean capture(Canvas canvas) {
		if (device == null) return false;
		return device.capture(canvas);
	}
}